﻿
using System.Collections.ObjectModel;
using System.Globalization;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Live;
using Microsoft.Live.Controls;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using WP7PhoneTracker.EntityClasses;
using WP7PhoneTracker.Resources;

namespace WP7PhoneTracker.UserControls
{
    public partial class Configuration_uc : UserControl
    {
        private LiveConnectClient _client;
        
        public Configuration_uc()
        {
            InitializeComponent();
            LangList.SelectedIndex = 0;
        }

        private void LangListSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var listBox = sender as ListBox;
            if (listBox != null)
            {
                string select = listBox.SelectedIndex.ToString(CultureInfo.InvariantCulture);

                string lang = " ";
                CultureInfo ci;

                //determine what culture is selected from UI
                switch (@select)
                {
                    case "0":
                        lang = "en-US";
                        break;
                    case "1":
                        lang = "es-ES";
                        break;
                    case "2":
                        lang = "de-DE";
                        break;
                }


                //Set application's current culture/langugae to UI selection
                ci = new CultureInfo(lang);
                Thread.CurrentThread.CurrentUICulture = ci;

            }

            ApplicationTitle.Text = AppResource.Configuration00;
            //txtBSignIn.Text = AppResource.Configuration01;

            if (App.Current.LiveSession != null)
            {
                txtLoginResult.Text = AppResource.Configuration02;
                txtWelcome.Text = txtWelcome.Text.Replace(txtWelcome.Text.Split(',')[0], AppResource.Configuration04.Replace(",",""));
            }
            else
            {
                txtLoginResult.Text = AppResource.Configuration03;
            }
                
            var pivotMain = Helpers.Helper.FindChild<Pivot>(Application.Current.RootVisual, "PivotMain");
            if (pivotMain != null) pivotMain.Title = AppResource.AppTitle;

            var pivotItem1 = Helpers.Helper.FindChild<PivotItem>(Application.Current.RootVisual, "PivotItem1");
            if (pivotItem1 != null) pivotItem1.Header = AppResource.PivotItem1;

            var pivotItem2 = Helpers.Helper.FindChild<PivotItem>(Application.Current.RootVisual, "PivotItem2");
            if (pivotItem2 == null) return;
            pivotItem2.Header = AppResource.PivotItem2;

            var trackingNameUc = ((UserControl)pivotItem2.FindName("TrackingNameUC"));
            if (trackingNameUc == null) return;
            var trackingNameUcApplicationTitle = (TextBlock)trackingNameUc.FindName("ApplicationTitle");

            if (trackingNameUcApplicationTitle != null)
                trackingNameUcApplicationTitle.Text = AppResource.TrackingList0;

            var trackingNameUctxtName = (TextBlock)trackingNameUc.FindName("txtName");
            if (trackingNameUctxtName != null)
                trackingNameUctxtName.Text = AppResource.CreateTrackingNameItem1;

            var trackingNameUcbtnSave = (Button)trackingNameUc.FindName("btnSave");
            if (trackingNameUcbtnSave != null)
                trackingNameUcbtnSave.Content = AppResource.CreateTrackingNameItem2;

            var trackingNameUctxtBError = (TextBlock)trackingNameUc.FindName("txtBError");
            if (trackingNameUctxtBError != null)
                trackingNameUctxtBError.Text = AppResource.CreateTrackingNameItem3;

            var dataContext = ((Model.TrakingName_Model) trackingNameUc.DataContext);
            if (dataContext == null) return;
            var result = dataContext.GetTrackingNameItems();
            if (result != null) 
                ((Model.TrakingName_Model) trackingNameUc.DataContext).TrackingNames = new ObservableCollection<TrackingName>(result);
        }

        private void BtnSigninSessionChanged(object sender, LiveConnectSessionChangedEventArgs e)
        {
            if (e.Status == LiveConnectSessionStatus.Connected)
            {
                _client = new LiveConnectClient(e.Session);
                App.Current.LiveSession = e.Session;
                txtLoginResult.Text = AppResource.Configuration02;
                txtWelcome.Visibility = Visibility.Visible;
                //txtBSignIn.Visibility = Visibility.Collapsed;

                _client.GetCompleted += OnGetCompleted;
                _client.GetAsync("me", null);
            }
            else
            {
                txtLoginResult.Text = AppResource.Configuration03;
                txtWelcome.Visibility = Visibility.Collapsed;
                //txtBSignIn.Visibility = Visibility.Visible;
                _client = null;
            }
        }

        void OnGetCompleted(object sender, LiveOperationCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                if (e.Result.ContainsKey("first_name") &&
                    e.Result.ContainsKey("last_name"))
                {
                    if (e.Result["first_name"] != null &&
                        e.Result["last_name"] != null)
                    {
                        txtWelcome.Text =
                            AppResource.Configuration04 + " " +
                            e.Result["first_name"] + " " +
                            e.Result["last_name"] + "!";
                    }
                }
                else
                {
                    txtWelcome.Text = AppResource.Configuration04 + AppResource.Configuration06;
                }
            }
            else
            {
                txtWelcome.Text = AppResource.Configuration05 +
                    e.Error;
            }
        }
    }
}